part of 'page.dart';

class _PronunciationSettingsPageView extends StatelessWidget {
  const _PronunciationSettingsPageView({
    required _PronunciationSettingsPageWloc wloc,
  }) : _wloc = wloc;

  final _PronunciationSettingsPageWloc _wloc;

  Widget _buildContent({
    required _PronunciationSettingsPageWlocState state,
    required BuildContext context,
  }) {
    if (state.availableAmVoices.length + state.availableBrVoices.length == 0) {
      return Container();
    }

    return PageTileHorizontalPadding(
      child: Column(
        children: [
          const TileGap(),
          _buildSpeechRateTileGroup(
            state: state,
            context: context,
          ),
          const TileGap(),
          _buildDialectTileGroup(
            state: state,
            context: context,
          ),
          const TileGap(),
          _buildVoiceTileGroup(
            state: state,
            context: context,
          ),
          const TileGap(),
        ],
      ),
    );
  }

  Widget _buildDialectTileGroup({
    required _PronunciationSettingsPageWlocState state,
    required BuildContext context,
  }) {
    return RoundedTileGroup(
      header: TileGroupHeader(
        title: PronunciationSettingsPageTranslation.of(context).dialectTileGroupHeader,
      ),
      tiles: [
        if (state.availableAmVoices.isNotEmpty)
          _buildDialectTile(
            state: state,
            dialect: Dialect.am,
            context: context,
          ),
        if (state.availableBrVoices.isNotEmpty)
          _buildDialectTile(
            state: state,
            dialect: Dialect.br,
            context: context,
          ),
      ],
    );
  }

  Widget _buildVoiceTileGroup({
    required _PronunciationSettingsPageWlocState state,
    required BuildContext context,
  }) {
    final footerTitle = switch (Config().platform) {
      Platform.ios => PronunciationSettingsPageTranslation.of(context).voiceTileGroupIosFooter,
      Platform.android =>
        PronunciationSettingsPageTranslation.of(context).voiceTileGroupAndroidFooter,
    };

    final voices = switch (state.dialect) {
      Dialect.am => state.availableAmVoices,
      Dialect.br => state.availableBrVoices,
    };

    final tiles = voices.map((voice) {
      return _buildVoiceTile(
        voice: voice,
        state: state,
      );
    }).toList();

    return RoundedTileGroup(
      header: TileGroupHeader(
        title: PronunciationSettingsPageTranslation.of(context).voiceTileGroupHeader,
      ),
      tiles: tiles,
      footer: TileGroupFooter(
        title: footerTitle,
      ),
    );
  }

  Widget _buildDialectTile({
    required Dialect dialect,
    required _PronunciationSettingsPageWlocState state,
    required BuildContext context,
  }) {
    final tileTitle = switch (dialect) {
      Dialect.am => PronunciationSettingsPageTranslation.of(context).amDialectTileTitle,
      Dialect.br => PronunciationSettingsPageTranslation.of(context).brDialectTileTitle,
    };

    Widget? trailing;

    if (dialect == state.dialect) {
      trailing = const TileTrailingCheck();
    }

    return PressableTextTile(
      title: tileTitle,
      trailing: trailing,
      onPressed: () {
        _wloc.onDialectTilePressed(
          dialect: dialect,
        );
      },
    );
  }

  Widget _buildVoiceTile({
    required TtsVoice voice,
    required _PronunciationSettingsPageWlocState state,
  }) {
    Widget? trailing;

    if (voice == state.voice) {
      trailing = const TileTrailingCheck();
    }

    return PressableTextTile(
      title: voice.title,
      trailing: trailing,
      onPressed: () {
        _wloc.onVoiceTilePressed(
          voice: voice,
        );
      },
    );
  }

  Widget _buildSpeechRateTileGroup({
    required _PronunciationSettingsPageWlocState state,
    required BuildContext context,
  }) {
    final iconColor = CoreTheme.of(context).textTheme.tileTitle.color;

    return RoundedTileGroup(
      header: TileGroupHeader(
        title: PronunciationSettingsPageTranslation.of(context).speechRateTileGroupHeader,
      ),
      tiles: [
        DefaultSizeTile(
          child: Container(
            color: CoreTheme.of(context).tileColor,
            child: Row(
              children: [
                const SizedBox(
                  width: 16,
                ),
                Icon(
                  CupertinoIcons.tortoise,
                  size: 26,
                  color: iconColor,
                ),
                const SizedBox(
                  width: 8,
                ),
                Expanded(
                  child: _SpeakingRateSlider(
                    value: state.normalizedSpeechRate,
                    onChangeEnd: _wloc.onSpeechRateSliderChangeEnd,
                  ),
                ),
                const SizedBox(
                  width: 8,
                ),
                Icon(
                  CupertinoIcons.hare,
                  size: 26,
                  color: iconColor,
                ),
                const SizedBox(
                  width: 16,
                ),
              ],
            ),
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: CoreTheme.of(context).backgroundColor,
      navigationBar: CupertinoNavigationBar(
        backgroundColor: CoreTheme.of(context).navigationBarColor,
        brightness: CoreTheme.of(context).brightness,
        automaticallyImplyLeading: false,
        automaticallyImplyMiddle: false,
        padding: EdgeInsetsDirectional.zero,
        middle: NavigationBarTitle(
          title: PronunciationSettingsPageTranslation.of(context).title,
        ),
        leading: AppBackButton(
          onPressed: Navigator.of(context).pop,
        ),
      ),
      child: SizedBox.expand(
        child: Scrollbar(
          child: SingleChildScrollView(
            child: WlocBuilder<_PronunciationSettingsPageWloc, _PronunciationSettingsPageWlocState>(
              wloc: _wloc,
              builder: (context, state) {
                return _buildContent(
                  state: state,
                  context: context,
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
